package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.YunLiuLiangServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;

@ChooseProvider(channel = "yunl")
public class YunLiuLiangAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(YunLiuLiangAdaptor.class);
	
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getIds();
		String callbackURL = BetweenProperties.CALLBACK_URL+"Yunll";
		log.info("between- 云流量订单YunLiuLiangService.order() 订购请求productId:" + productId+",phoneNo:"+phoneNo+",cstmOrderNo:"+cstmOrderNo+",callbackURL"+callbackURL);
		String result = YunLiuLiangServiceImpl.service.order(productId, phoneNo, cstmOrderNo, callbackURL);
		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("resultCode");
	

		if (("00".equals(code))) {
			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("11");
			orderBean.setSuppOrderNo(String.valueOf(json.get("orderId").toString()));
		} else {
			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg("订购失败");
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();

		String cstmOrderNo =orderBean.getIds();
		log.info("between -云流量状态YunLiuLiangService.orderStatus() 查询请求cstmOrderNo:" + cstmOrderNo);
		String result = YunLiuLiangServiceImpl.service.orderStatus(cstmOrderNo);

		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("resultCode");

	
		if (("00".equals(code))) {
			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg("订购成功");
			orderBean.setStatus("7");
		} else if (("01".equals(code))) {
			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("1");
		} else {
			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg((String) json.get("resultMsg"));
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
